package com.nwpu.intimacy.adapter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.nwpu.intimacy.R;
import com.nwpu.intimacy.been.ChatFriendInformation;

public class FriendMessageAdapter extends BaseAdapter {

	private List<ChatFriendInformation> lists;
	private Context mContext;
	private RelativeLayout layout;

	public FriendMessageAdapter(List<ChatFriendInformation> lists,
			Context mContext) {

		this.lists = lists;
		this.mContext = mContext;

	}

	@Override
	public int getCount() {
		return lists.size();
	}

	@Override
	public Object getItem(int position) {
		return lists.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		LayoutInflater inflater = LayoutInflater.from(mContext);
		layout = (RelativeLayout) inflater.inflate(R.layout.chat_item, null);

		ImageView imageHeader = (ImageView) layout
				.findViewById(R.id.head_user_chat);
		TextView userName = (TextView) layout
				.findViewById(R.id.chat_friend_name);
		TextView time = (TextView) layout.findViewById(R.id.chat_time);
		TextView message = (TextView) layout.findViewById(R.id.chat_message);

		Drawable drawable = new BitmapDrawable(lists.get(position)
				.getUserPhoto());
		imageHeader.setImageDrawable(drawable);

		userName.setText(lists.get(position).getUsername());
		if (!lists.get(position).getTime().equals("")) {

			time.setText(lists.get(position).getTime().split(" ")[1]);

		} else {

			time.setText(getTime(System.currentTimeMillis()));

		}

		message.setText(lists.get(position).getMessage());

		return layout;
	}

	/**
	 * 时间转换
	 * 
	 * @param currentTime
	 * @return
	 */
	@SuppressLint("SimpleDateFormat")
	private String getTime(double currentTime) {

		SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
		Date curDate = new Date();
		String str = format.format(curDate);
		return str;

	}

}
